<?php

/**
 * hook钩子触发短信发送
 */

namespace app\common\behavior;

//阿里云短信发送相关类 composer安装 
//composer require alibabacloud/sdk
use Swoft\Task\Bean\Annotation\Task;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\Exception;

class Sms
{

    //发送验证码
    //@sms  验证码发送记录数数据库实例
    //$sms->mobile 手机号码
    //['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]
    function smsSend($sms)
    {
        $e = $this->sendMsg($sms->mobile, ['code' => $sms->code], 'code');
        return $e;
    }

    /**
     * 通知门店
     */
    function smsNotice($sms)
    {
        // var_dump($sms);die;
        if ($sms['mobile']) {
            $arr  = explode(',', $sms['mobile']);
            foreach ($arr as $key => $m) {
                try {
                    $e = $this->sendMsg($m, ['name' => $sms['msg'] ?? '用户'], $sms['template'] ?? 'notice_shop');
                } catch (\Throwable $th) {
                    //throw $th;
                }
            }
        }
        return true;
    }


    // /**
    //  * 通知用户
    //  */
    // function smsNoticeUser($sms)
    // {
    //     // var_dump($sms);die;
    //     if($sms['mobile']){
    //         $arr  = explode(',',$sms['mobile']);
    //         foreach ($arr as $key => $m) {
    //             $e = $this->sendMsg($m, ['code' => $sms['msg']??'222'], 'notice_user');

    //         }
    //     }
    //      return $e;
    // }

    /*function smsCheck($sms)
    {	
    	return $this->sendMsg($sms->mobile,['code'=>$sms->code],'code');
    }*/


    //@mobile 手机号码
    //@param  短信发送参数
    //@template 短信发送模板编号key值
    private function sendMsg($mobile, $param, $template)
    {
        $config = \config('alisms');

        if (empty($param) || !isset($config) || empty($config)) {
            return false;
        }
        AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
            ->regionId($config['regionId'])
            ->asGlobalClient();

        $TemplateCode = isset($config['TemplateCode'][$template]) ? $config['TemplateCode'][$template] : $config['TemplateCode']['code'];

        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'PhoneNumbers' => $mobile,
                        'SignName' => $config['SignName'],
                        'TemplateCode' => $TemplateCode,
                        'TemplateParam' => json_encode($param)
                    ],
                ])
                ->request();

            if ($result->Code == "OK") {
                return true;
            } else {
                exception($result->Message);
            }
        } catch (Exception $e) {
            exception($e->getMessage());
        } catch (ClientException $e) {
            exception($e->getErrorMessage());
        } catch (ServerException $e) {
            exception($e->getErrorMessage());
        }

        return false;
    }
}
